package educrypt.commons.util;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.List;

public class Util {
	public static int safeLongToInt(long l) {
		if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
			throw new IllegalArgumentException(l
					+ " cannot be cast to int without changing its value.");
		}
		return (int) l;
	}

	public static char[] byteArrayToCharArray(byte[] source) {
		int resultSize = source.length;
		char[] result = new char[resultSize];
		for (int i = 0; i < resultSize; i++) {
			result[i] = (char) source[i];
		}
		return result;
	}

	public static byte[] charArrayToByteArray(char[] source) {
		int resultSize = source.length;
		// Math.round((float) source.length / 2.0f);
		byte[] result = new byte[resultSize];
		for (int i = 0; i < resultSize; i++) {
			result[i] = (byte) source[i];
		}
		return result;
	}

	public static void restartApplication(Runnable runBeforeRestart,
			String[] args) throws IOException {
		final String SUN_JAVA_COMMAND = "sun.java.command";
		try {
			// java binary
			String java = System.getProperty("java.home") + "/bin/java";
			// vm arguments
			List<String> vmArguments = ManagementFactory.getRuntimeMXBean()
					.getInputArguments();
			StringBuffer vmArgsOneLine = new StringBuffer();
			for (String arg : vmArguments) {
				// if it's the agent argument : we ignore it otherwise the
				// address of the old application and the new one will be in
				// conflict
				if (!arg.contains("-agentlib")) {
					vmArgsOneLine.append(arg);
					vmArgsOneLine.append(" ");
				}
			}
			// init the command to execute, add the vm args
			final StringBuffer cmd = new StringBuffer("" + java + " "
					+ vmArgsOneLine);

			// program main and program arguments
			String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(
					" ");
			// program main is a jar
			if (mainCommand[0].endsWith(".jar")) {
				// if it's a jar, add -jar mainJar
				cmd.append("-jar " + new File(mainCommand[0]).getPath());
			} else {
				// else it's a .class, add the classpath and mainClass
				cmd.append("-cp " + System.getProperty("java.class.path")
						+ " " + mainCommand[0]);
			}
			// finally add program arguments
			for (int i = 0; i < args.length; i++) {
				cmd.append(" ");
				cmd.append(args[i]);
			}

			// execute the command in a shutdown hook, to be sure that all the
			// resources have been disposed before restarting the application
			Runtime.getRuntime().addShutdownHook(new Thread() {
				@Override
				public void run() {
					try {
						Runtime.getRuntime().exec(cmd.toString());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			});
			// execute some custom code before restarting
			if (runBeforeRestart != null) {
				runBeforeRestart.run();
			}
			// exit
			System.exit(0);
		} catch (Exception e) {
			// something went wrong
			throw new IOException(
					"Error while trying to restart the application", e);
		}
	}
}
